﻿using Approval.Domain;
using MediatR;
using PublicCommon;
using T_Material.API.Application.Commands.RequisitionCommand;
using T_Material.Domain;
using T_Material.Infrastructure.ImpI;
using T_Material.Infrastructure.Interface;

namespace T_Material.API.Application.CommandHandlers.RequisitionHandlers
{
    public class RequisitionCreatedStateCommandHandler : IRequestHandler<RequisitionCreatedStateCommand, int>
    {
        private readonly IRequisitionRepository requisitionRepository;
        private readonly Cross_services_Helper cross_Services_Helper;
        public RequisitionCreatedStateCommandHandler(IRequisitionRepository requisitionRepository, Cross_services_Helper cross_Services_Helper)
        {
            this.requisitionRepository = requisitionRepository;
            this.cross_Services_Helper = cross_Services_Helper;
        }
        public async Task<int> Handle(RequisitionCreatedStateCommand request, CancellationToken cancellationToken)
        {
            var info = new Requisition();
            info.Id = request.Id;
            info.Title = request.Title;
            info.Recipient = "赵六";
            info.Remarks = request.Remarks;
            info.RequisitionDate = DateTime.Now;
            info.UsageDescription = request.UsageDescription;
            info.ApprovedBy = request.ApprovedBy;
            info.CollectingItems = request.CollectingItems;
            info.GoodsName = request.GoodsName;
            info.TypeName = request.TypeName;
            info.Description = request.Description;
            info.Specifications = request.Specifications;
            info.Country = request.Country;
            info.Province = request.Province;
            info.WarehouseCount = request.WarehouseCount;
            info.ReceivedCount = request.ReceivedCount;
            info.AuditState = 1;
            info.CreateBy = "张三";
            info.CreateDate = DateTime.Now;
            info.UpdateBy = "";
            info.UpdateDate = null;
            info.IsDel = false;
            await requisitionRepository.Addsync(info);

            var obj = new AcceptanceRelation();
            obj.AId = info.Id.ToString();
            obj.ApprovalId = 4;
            obj.ApprovalName = "小明";
            var message = await cross_Services_Helper.Post("http://10.31.60.9:8000/Approval/api/AcceptanceRelation/AcceptanceRelationAdd", obj);
            if (message.LastIndexOf("状态码")>0)
            {
                return await Task.FromResult(-1);
            }

            return await Task.FromResult(info.Id);
        }
    }
}
